<template>
  <el-select
    ref="select"
    v-bind="$attrs"
    v-model="selectValue"
    v-on="$listeners"
    class="role-employee-select"
    popper-class="role_select"
    @change="selectChange"
  >
    <div class="absolute_ipt">
      <el-input
        prefix-icon="el-icon-search"
        placeholder="搜索角色"
        :validate-event="false"
        v-model="filterText"
      >
      </el-input>
    </div>
    <div class="role-employee-select__body select_body">
      <el-option
        v-for="item in roleOption"
        :key="item.roleId"
        :label="item.roleName"
        :value="item.roleId"
      />
      <p v-if="roleOption && roleOption.length == 0">暂无数据</p>
      <!-- 解决搜索项为空时，下拉不显示的bug -->
      <el-option
        v-for="(item) in groupList"
        :label="item.name"
        :key="item.deptId"
        :value="item.deptId"
        style="opacity: 0; height: 0"
      />
    </div>
  </el-select>
</template>

<script>
import { getAllBaseRoleList } from "@/api/admin/employeeDep";
import merge from "@/utils/merge";

const DefaultProps = {
  onlyShowRole: false, // 仅展示角色
};

export default {
  // 角色选择
  name: "UserEmployeeSelect",
  props: {
    props: {
      type: Object,
      default: () => {
        return {};
      },
    },
    value: [Array, Number, String],
  },

  data() {
    return {
      filterText: "",
      roleOption: [],
      tempRoleOptions: [],
      selectValue: [],
      groupList: [
        {
          deptId: -1,
          name: "站位数据",
        },
      ],
    };
  },

  computed: {
    config() {
      return merge({ ...DefaultProps }, this.props || {});
    },
    select() {
      return this.$refs.select;
    },
  },

  watch: {
    // 角色列表搜索过滤
    filterText(val) {
      let list = [];
      if (val != "" || val != null) {
        this.tempRoleOptions.forEach((item) => {
          if (item.roleName.includes(val)) {
            list.push(item);
          }
        });
        this.roleOption = list;
      } else {
        this.roleOption = this.tempRoleOptions;
      }
    },
  },
  created() {
    this.getRoleList();
    if (typeof this.value === "string" && this.value != "") {
      this.selectValue = JSON.parse(this.value);
    } else if (typeof this.value === "object" || typeof this.value === "number") {
      this.selectValue = this.value;
    }
  },
  methods: {
    /**
     * 获取角色列表
     */
    getRoleList() {
      getAllBaseRoleList()
        .then((res) => {
          // 过滤数据
          this.roleOption = this.filter(res.data) || [];
          // 用户搜索过滤的数据
          this.tempRoleOptions = this.roleOption;
        })
        .catch(() => {});
    },
    // 数据过滤方法
    filter(data) {
      const list = [];
      data.forEach((item) => {
        list.push(...item.list);
      });
      return list;
    },
    selectChange() {
      this.$emit("input", this.selectValue);
    },
  },
};
</script>
<style lang="scss">
.el-tabs__header--hidden {
  .el-tabs__header {
    display: none;
  }
}
</style>

<style lang="scss" scoped>
.role-employee-select {
  &__body {
    padding: 0 15px;
    width: 100%;
    p {
      text-align: center;
      color: #999;
      height: 60px;
      line-height: 60px;
    }
  }

  &__title {
    border-bottom: 1px solid #e4e7ed;
    padding: 0 0 7px;
    font-size: 12px;
    color: #909399;
    line-height: 30px;
  }

  .el-select-dropdown__item {
    padding: 0 10px;
  }
}

.el-select-dropdown.is-multiple .el-select-dropdown__item.selected::after {
  content: "" !important ;
}

.el-select-dropdown.is-multiple .el-select-dropdown__item.selected::before {
  border: 1px solid #2362fb;
  color: #fff;
  background: #2362fb;
}

.el-select-dropdown.is-multiple .el-select-dropdown__item::before {
  position: absolute;
  left: 0;
  top: 8px;
  font-family: "element-icons";
  content: "\E6DA";
  font-size: 12px;
  font-weight: bold;
  -webkit-font-smoothing: antialiased;
  border: 1px solid #c1c7ce;
  width: 14px;
  display: block;
  height: 14px;
  margin: 0;
  line-height: 14px;
  color: transparent;
}

/deep/ .el-tabs {
  .el-tabs__nav-wrap::after {
    height: 1px;
  }

  .el-tab-pane {
    height: 100%;
    overflow-y: auto;
  }

  .el-tabs__content {
    height: 200px;
  }
}

.search-input {
  margin-bottom: 5px;
  /deep/ .el-input__inner {
    background-color: #f4f4f4;
    border: none;
  }
}

.cell {
  width: calc(100% - 30px);
  &__img {
    margin-right: 8px;
    vertical-align: middle;
    flex-shrink: 0;
  }
  &__body {
    flex-shrink: 0;
    flex: 1;
    margin-right: 30px;
  }
  &__footer {
    text-align: right;
  }
}
.role-employee-select__body /deep/ .el-select-dropdown__item span {
  font-size: 13px;
}
</style>
<style lang="scss">
// 修改角色选择器下拉高度
.role_select {
  .el-select-dropdown__wrap {
    max-height: 378px !important;
    .el-input__inner {
      border: none !important;
      background: #f6f7fc !important;
    }
  }
}
</style>
<style  lang="scss" scoped>

.role-employee-select /deep/ .el-tag.el-tag--info {
  color: #333333 !important;
}
@import "../../styles/selector.scss"
</style>
